/**
 * Copyright (c) 2002-2011 旭鸣信息
 * All rights reserved. 
 * File：ImgViewerContorller.java
 * History:
 *       May 22, 2012: Initially created, liuzhi.
 */
package com.xteam.ecsystem.contorller;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.tangkf.utils.bean.ReqHelper;


/**
 * 文件显示处理枢纽站
 * @author liuzhi
 *
 */
@Controller
public class ImgViewerContorller {
	
	//设定输出的类型
	public static final String GIF="image/gif;charset=GB2312";
	public static final String JPG="image/jpeg;charset=GB2312";
	public static final String PNG="image/png;charset=GB2312";
	
	public void setToBeJsonType(HttpServletRequest request, HttpServletResponse response) {
		response.setContentType("application/json;charset=UTF-8");
	};
	
	/**
	 * 处理并显示上传图片
	 * @author liuzhi
	 * @param request
	 * @param response
	 * @param modelMap
	 * @throws IOException
	 */
	@RequestMapping("/imgViewerContorller.do")
	public void imgViewerContorller(HttpServletRequest request,
			HttpServletResponse response, ModelMap modelMap) throws IOException {
		String basePath = request.getParameter("basePath");
		String imgPath = request.getParameter("newImgPath");
		imgPath = basePath + imgPath;
		
		try {
			File img = new File(imgPath);
			if(imgPath.toLowerCase().endsWith("gif")){
				response.setContentType(GIF);
			}else if(imgPath.toLowerCase().endsWith("png")){
				response.setContentType(PNG);
			}else{
				response.setContentType(JPG);
			}
			
			InputStream imageIn = new FileInputStream(img);
			//输入缓冲流
			BufferedInputStream bis = new BufferedInputStream(imageIn);	
			//输出缓冲流
			BufferedOutputStream bos = new BufferedOutputStream(response.getOutputStream());	
			// 缓冲字节数
			byte data[] = new byte[4096];	
			int size = 0;
			size = bis.read(data);
			while (size != -1) {
				bos.write(data, 0, size);
				size = bis.read(data);
			}
			
			//清空输出缓冲流
			bos.flush();	
			bos.close();
			bis.close();
			imageIn.close();
		} catch (Exception e) {
			System.err.println("图片显示错误："+e.getMessage());
		}
	}
	
	/**
	 * 验证同级目录下面是否存在某个相同的文件名
	 * @author liuzhi
	 * @param request
	 * @param response
	 * @param modelMap
	 * @throws IOException
	 */
	@RequestMapping("/isExistingFileName.do")
	public void isExistingFileName(HttpServletRequest request,
			HttpServletResponse response, ModelMap modelMap) throws IOException {
		setToBeJsonType(request, response);
		JSONObject info = new JSONObject();
		
		String basePath = ReqHelper.getRequestString(request, "basePath");
		String baiAttachname = ReqHelper.getRequestString(request, "v_attachName");
		String baiAttachurl = ReqHelper.getRequestString(request, "v_attachUrl");
		
		baiAttachurl = baiAttachurl.substring(0, (baiAttachurl.lastIndexOf("/") + 1));
		String fileRealPath = basePath + baiAttachurl + baiAttachname;
		
		try {
			File file = new File(fileRealPath);
			if (file.exists()) {
				info.put("warning", "该目录下已经存在名称为" + baiAttachname + "的附件名！");
			}
			
		} catch (Exception e) {
			System.err.println("读取文件出错！");
		}
		
		response.getWriter().write(info.toString());
	}

}
